package com.fieldchain.ex.fa100.ui.c2c.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.bean.C2CListBean;
import com.fieldchain.ex.fa100.ui.c2c.activity.C2CBuyDetailsActivity;
import com.fieldchain.ex.fa100.ui.c2c.activity.C2CSellDetailsActivity;
import com.fieldchain.ex.fa100.utils.common.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/13
 */
public class C2CHistoryAdapter extends RecyclerView.Adapter<C2CHistoryAdapter.ViewHolder> {
    private Context mContext;
    private String[] tabs = new String[]{"未付款", "已付款", "已取消", "已审核", "已驳回", "已过期"};

    public C2CHistoryAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public List<C2CListBean> getList() {
        return list;
    }

    private List<C2CListBean> list = new ArrayList<>();

    public void setData(List<C2CListBean> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_c2chistory, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final C2CListBean bean = list.get(position);
        String t1 = "";
        String t2 = "";
        String t3 = "";
        int c = 0;
        final int orderType = bean.getOrderType();
        if (orderType == 1) {
            t1 = "支出";
            t2 = "+";
            c = Color.parseColor("#FFDC3950");
            tabs[0] = "未付款";
        } else {
            t1 = "收入";
            t2 = "-";
            c = Color.parseColor("#FF00B05F");
            tabs[0] = "未确认";
        }

        holder.linHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = null;
                if (orderType == 1) {
                    intent = new Intent(mContext, C2CBuyDetailsActivity.class);
                } else {
                    intent = new Intent(mContext, C2CSellDetailsActivity.class);
                }
                intent.putExtra("id", bean.getId());
                mContext.startActivity(intent);

            }
        });
        holder.tvTime.setText(TimeUtils.getSimpleTime2(bean.getCreateTime() + ""));
        holder.tvStatus.setText(tabs[bean.getStatus()]);
        holder.tvNum.setText(bean.getAmount() + "");
        holder.tvType.setText(t1);
        holder.tvPrice.setText(t2 + bean.getExchangeNumber() + " " + bean.getCoinName().toUpperCase());
        holder.tvPrice.setTextColor(c);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout linHistory;
        private TextView tvTime;
        private TextView tvPrice;
        private TextView tvType;
        private TextView tvNum;
        private TextView tvStatus;

        public ViewHolder(View itemView) {
            super(itemView);
            linHistory = itemView.findViewById(R.id.lin_history);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvType = itemView.findViewById(R.id.tv_type);
            tvNum = itemView.findViewById(R.id.tv_num);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }
}
